home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / amiga / uae-0.7.0b2 / src / od-linux / joystick.c < prev    next >
C/C++ Source or Header  |  1998-01-20  |  2KB  |  111 lines

  1.  /* 
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Joystick emulation for Linux
  5.   * 
  6.   * Copyright 1997 Bernd Schmidt
  7.   */
  8.  
  9. #include "sysconfig.h"
  10. #include "sysdeps.h"
  11.  
  12. #include "config.h"
  13. #include "options.h"
  14. #include "memory.h"
  15. #include "custom.h"
  16. #include "joystick.h"
  17.  
  18. int nr_joysticks;
  19.  
  20. static int js0, js1;
  21.  
  22. #ifdef HAVE_LINUX_JOYSTICK_H
  23.  
  24. #if BROKEN_JOYSTICK_H == 1
  25. #include "ljoystick.h"
  26. #else
  27. #include <linux/joystick.h>
  28. #endif
  29.  
  30. #if OLD_JOYSTICK_H == 0
  31. #define JS_DATA_TYPE js_status
  32. #endif
  33.  
  34. static struct JS_DATA_TYPE jscal;
  35.  
  36. void read_joystick(int nr, unsigned int *dir, int *button)
  37. {
  38.     static int minx = MAXINT, maxx = MININT,
  39.                miny = MAXINT, maxy = MININT;
  40.     int left = 0, right = 0, top = 0, bot = 0;
  41.     struct JS_DATA_TYPE buffer;
  42.     int len;
  43.     int fd = nr == 0 ? js0 : js1;
  44.  
  45.     *dir = 0;
  46.     *button = 0;
  47.     if (nr >= nr_joysticks)
  48.         return;
  49.     
  50.     len = read(fd, &buffer, sizeof(buffer));
  51.     if (len != sizeof(buffer)) 
  52.         return;
  53.     
  54.     if (buffer.x < minx) minx = buffer.x;
  55.     if (buffer.y < miny) miny = buffer.y;
  56.     if (buffer.x > maxx) maxx = buffer.x;
  57.     if (buffer.y > maxy) maxy = buffer.y;
  58.     
  59.     if (buffer.x < (minx + (maxx-minx)/3))
  60.         left = 1;
  61.     else if (buffer.x > (minx + 2*(maxx-minx)/3))
  62.         right = 1;
  63.  
  64.     if (buffer.y < (miny + (maxy-miny)/3))
  65.         top = 1;
  66.     else if (buffer.y > (miny + 2*(maxy-miny)/3))
  67.         bot = 1;
  68.         
  69.     if (left) top = !top;
  70.     if (right) bot = !bot;
  71.     *dir = bot | (right << 1) | (top << 8) | (left << 9);
  72.     *button = buffer.buttons & 3;
  73. }
  74.  
  75. #else
  76.  
  77. void read_joystick(int nr, unsigned int *dir, int *button)
  78. {
  79.     *dir = 0;
  80.     *button = 0;
  81. }
  82.  
  83. #endif
  84.  
  85. void init_joystick(void)
  86. {
  87.     nr_joysticks = 0;
  88.     js0 = -1; js1 = -1;
  89.     js0 = open("/dev/js0", O_RDONLY);
  90.     if (js0 < 0) {
  91.     write_log ("No joysticks found\n");
  92.         return;
  93.     }
  94.     nr_joysticks = 1;
  95.     js1 = open("/dev/js1", O_RDONLY);
  96.     if (js1 < 0) {
  97.     write_log ("Found one joystick\n");
  98.         return;
  99.     }
  100.     write_log ("Found two joysticks\n");
  101.     nr_joysticks = 2;
  102. }
  103.  
  104. void close_joystick(void)
  105. {
  106.     if (js0 >= 0)
  107.     close(js0);
  108.     if (js1 >= 0)
  109.     close(js1);
  110. }
  111.